package com.globallogic.socialnetwork.jsf.bean;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.inject.Named;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;

import com.globallogic.socialnetwork.entity.Group;
import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.service.SearchService;
import com.globallogic.socialnetwork.service.UserService;
import com.globallogic.socialnetwork.util.DaoException;
import com.globallogic.socialnetwork.util.WebApplication;

/**
 * JSF Bean to handle searching of users
 */
@Named("searchBean")
@Scope("session")
public class SearchBean {

	@EJB(mappedName = "java:module/UserService")
	private UserService userService;
	private String filter;
	private String condition;
	private List<UserBean> users;
	private List<Group> groups;
	private User selectedUser;
	private Group selectedGroup;
	private String userLogin;
	private String groupName;
	private final static Logger log = Logger
			.getLogger("SocialNetworkFileLogger");

	public SearchBean() {
		userLogin = WebApplication.getSessionAttribute("userlogin").toString();
	}

	/**
	 * Searches users that are not friends
	 * 
	 * @return "searchresult"
	 */
	@SuppressWarnings("unchecked")
	public String search() {
		try {
			List<User> result = userService.getUsersByFilter(filter, condition);
			List<User> friends = userService.getFriends(userLogin);
			friends.add(userService.getUser(userLogin));
			List<User> allUsers = ListUtils.subtract(result, friends);
			if (allUsers != null) {
				users = new ArrayList<UserBean>();
				UserBean userBean;
				for (User user : allUsers) {
					userBean = new UserBean(user);
					users.add(userBean);
				}
			} else {
				users = null;
			}
		} catch (DaoException e) {
			log.error(e);
		}
		return "searchresult";
	}

	/**
	 * Sends request to <selectedUser> on adding to friends
	 * 
	 * @return "home"
	 */
	public String follow() {
		try {
			userService.followUser(userLogin, selectedUser);
		} catch (DaoException e) {
			log.error(e);
		}
		return "home";
	}

	/**
	 * Sends query on joing to group
	 * 
	 * @return "home"
	 */
	public String joinGroup() {
		try {
			User user = userService.getUser(userLogin);
			List<Group> groups = userService.getUserGroups(user);
			groups.add(selectedGroup);
			user.setGroups(groups);
			userService.updateUser(user);
		} catch (DaoException e) {
			log.error(e);
		}
		return "home";
	}

	/**
	 * Searches groups by name <groupName>
	 * @throws MalformedURLException 
	 */
	public void searchGroup() throws MalformedURLException {
		URL url = new URL(
				"http://localhost:7001/VatslavskaSteshov-SocialNetwork/SearchServiceImplService?wsdl");
		QName qname = new QName(
				"http://service.socialnetwork.globallogic.com/",
				"SearchServiceImplService");

		Service service = Service.create(url, qname);
		SearchService serv = service.getPort(SearchService.class);
		try {
			groups = serv.searchGroups(userLogin,groupName);
		} catch (DaoException e) {
			log.error(e);
		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<UserBean> getUsers() {
		return users;
	}

	public void setUsers(List<UserBean> users) {
		this.users = users;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}

	public Group getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(Group selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

}
